﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TTQT.TTQT_Code;

public partial class but_SANPHAM_ListSanPham : System.Web.UI.UserControl
{
    public string NhomId
    {
        set { Session["nhomId"] = value; }
        get
        {
            if (Session["nhomId"] == null)
            {
                Session["nhomId"] = "-1";
                return "-1";
            }
            return Session["nhomId"].ToString();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            string nhomId = Request["nhomId"];
            if(String.IsNullOrEmpty(nhomId)){
                NhomId = "-1";
            }
            else
            {
                NhomId = nhomId;
            }
            //bindGrid("4", "-1");
            bindGridPaging(NhomId, "1");
        }
    }
    //private void bindGrid(string nhomId,string iActive)
    //{
    //    SanPhamDAO sp=new SanPhamDAO();
    //    //rptSanPham.DataSource = sp.sanPHam_GetByNhomId(nhomId, iActive);
    //    rptSanPham.DataSource = sp.sanPHam_GetByNhomId_Paging(nhomId, iActive, pager1.PageSize, pager1.CurrentIndex);
    //    rptSanPham.DataBind();
    //}
    protected void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        pager1.CurrentIndex = currnetPageIndx;
        bindGridPaging(NhomId, "-1");
    }
    public string firstWords(string inputStr,int numberOfChar)
    {
        //try
        //{
        //    return Regex.Match(inputStr, @"^(\w+\b.*?){" + numberOfWord + "}") + "...";
        //}catch
        //{
        //    return inputStr;
        //}
        if (inputStr.Length <= numberOfChar)
            return inputStr;
        try
        {
            inputStr = inputStr.Substring(0, numberOfChar);
            inputStr = inputStr.Substring(0, inputStr.LastIndexOf(" "));
        }catch
        {
            return inputStr;
        }
        return inputStr+"...";
    }
    public void bindGridPaging(string nhomId, string iActive)
    {
        SanPhamDAO sp=new SanPhamDAO();
        PagingOpbject obj = sp.sanPham_GetByNhomId_Paging(nhomId, iActive,pager1.PageSize,pager1.CurrentIndex);
        rptSanPham.DataSource = obj.tableReturn;
        rptSanPham.DataBind();
        pager1.ItemCount = obj.totalRecords;
    }
    public void bindGridPaging2(string nhomId, string iActive) //Ham nay chay duoc
    {
        DataTable result = null;

        SqlConnection conn = new SqlConnection(TTQTCommon.DBConnectionString);
        SqlCommand comm = new SqlCommand("sanPHam_GetByNhomId_Paging", conn);

        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.Clear();
        comm.Parameters.Add("@NhomId", SqlDbType.Int, 4).Value = nhomId;
        comm.Parameters.Add("@iActive", SqlDbType.Int, 4).Value = iActive;

        comm.Parameters.Add("@PageSize", SqlDbType.Int, 4).Value = pager1.PageSize;
        comm.Parameters.Add("@CurrentPage", SqlDbType.Int, 4).Value = pager1.CurrentIndex;
        comm.Parameters.Add("@ItemCount", SqlDbType.Int).Direction = ParameterDirection.Output;
        try
        {
            conn.Open();
            result = new DataTable();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(result);
            rptSanPham.DataSource = result;
            rptSanPham.DataBind();
            int totalRecords = Convert.ToInt32(comm.Parameters["@ItemCount"].Value);
            pager1.ItemCount = totalRecords;
        }
        catch (Exception exx)
        {
            // Writing log methods here.
        }
        finally
        {
            if (conn.State == ConnectionState.Open) conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
    }
}
